package bg.cinema_system.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import bg.cinema_system.beans.MovieBean;
import bg.cinema_system.global.ConnectionJDBC;

public class MovieDAO {

	// Connect to DB and get information about the selected Movie
	public static MovieBean getMovie(long movie_id) throws SQLException {
		// Get database connection
		Connection dbConnection = ConnectionJDBC.getConnection();

		if (dbConnection == null) {
			throw new SQLException(
					"Class MovieDAO: Can't get database connection");
		}

		String sqlStatement = "SELECT movies.movie_id, movies.name, movies.movie_type, movies.duration, movies.director, movies.actors, movies.country, movies.summary "
				+ "FROM movies " + "WHERE movies.movie_id = ?";

		PreparedStatement ps = dbConnection.prepareStatement(sqlStatement);
		ps.setLong(1, movie_id);

		// Get customer data from database
		ResultSet result = ps.executeQuery();

		MovieBean movie = new MovieBean();

		while (result.next()) {
			movie.setMovie_id(result.getLong("movie_id"));
			movie.setName(result.getString("name"));
			movie.setMovie_type(result.getString("movie_type"));
			movie.setDuration(result.getTime("duration"));
			movie.setDirector(result.getString("director"));
			movie.setActors(result.getString("actors"));
			movie.setCountry(result.getString("country"));
			movie.setSummary(result.getString("summary"));
		}

		// Close all connections
		ps.close();
		result.close();
		dbConnection.close();

		return movie;
	}
}
